﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace HMIS.FrameWork.Common
{
	public class SendSms
	{
		protected static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
		// 短信发送地址
		protected static string smsurl = System.Web.Configuration.WebConfigurationManager.AppSettings["smsurl"];
		// 请求时发送名称
		protected static string sysnam = System.Web.Configuration.WebConfigurationManager.AppSettings["sysnam"];

		/// <summary>
		/// 发送添加好友请求
		/// </summary>
		/// <param name="mobile"></param>
		/// <param name="content"></param>
		/// <returns></returns>
		public static string SendAddFetionByGetRequest(string mobile)
		{
			string nickname = System.Web.HttpUtility.UrlEncode(sysnam, Encoding.GetEncoding("GBK"));
			string url = smsurl + "&action=add&sendto=" + mobile + "&nickname=" + nickname;
			string getReturn = doGetRequest(url);
			logger.Info("SMS send : [" + url + "], " + getReturn);
			return getReturn;
		}

		/// <summary>
		/// 通过Http Get方式发送短信
		/// </summary>
		/// <param name="mobile"></param>
		/// <param name="content"></param>
		/// <returns>返回Http信息</returns>
		public static string SendByGetRequest(string mobile, string content)
		{
			content = System.Web.HttpUtility.UrlEncode(content, Encoding.GetEncoding("GBK"));
			string url = smsurl + "&sendto=" + mobile + "&message=" + content;
			string getReturn = doGetRequest(url);
			logger.Info("SMS send : [" + url + "], " + getReturn);
			return getReturn;
		}

		/// <summary>
		/// 通过Http Post方式发送短信
		/// </summary>
		/// <param name="mobile"></param>
		/// <param name="content"></param>
		/// <returns>返回Http信息</returns>
		public static string SendByPostRequest(string mobile, string content)
		{
			string url = smsurl.Substring(0, smsurl.IndexOf('?'));
			string par = smsurl.Substring(smsurl.IndexOf('?') + 1);
			par = par + "&sendto=" + mobile + "&message=" + content;
			byte[] bTemp = System.Text.Encoding.GetEncoding("GBK").GetBytes(par + content);
			string postReturn = doPostRequest(url, bTemp);
			logger.Info("SMS send : [" + url + "], [" + par + content + "], " + postReturn);
			return postReturn;
		}

		//POST方式发送得结果
		private static string doPostRequest(string url, byte[] bData)
		{
			HttpWebRequest hwRequest;
			HttpWebResponse hwResponse;
			string strResult = string.Empty;
			try
			{
				hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
				hwRequest.Timeout = 5000;
				hwRequest.Method = "POST";
				hwRequest.ContentType = "application/x-www-form-urlencoded";
				hwRequest.ContentLength = bData.Length;

				System.IO.Stream smWrite = hwRequest.GetRequestStream();
				smWrite.Write(bData, 0, bData.Length);
				smWrite.Close();
			}
			catch (System.Exception ee)
			{
				logger.Error(ee);
				return ee.Message;
			}

			//get response
			try
			{
				hwResponse = (HttpWebResponse)hwRequest.GetResponse();
				StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
				strResult = srReader.ReadToEnd();
				srReader.Close();
				hwResponse.Close();
			}
			catch (System.Exception ee)
			{
				logger.Error(ee);
				return ee.Message;
			}

			return strResult;
		}

		//GET方式发送得结果
		private static String doGetRequest(string url)
		{
			HttpWebRequest hwRequest;
			HttpWebResponse hwResponse;

			string strResult = string.Empty;
			try
			{
				hwRequest = (System.Net.HttpWebRequest)WebRequest.Create(url);
				hwRequest.Timeout = 5000;
				hwRequest.Method = "GET";
				hwRequest.ContentType = "application/x-www-form-urlencoded";
			}
			catch (System.Exception ee)
			{
				logger.Error(ee);
				return ee.Message;
			}

			//get response
			try
			{
				hwResponse = (HttpWebResponse)hwRequest.GetResponse();
				StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
				strResult = srReader.ReadToEnd();
				srReader.Close();
				hwResponse.Close();
			}
			catch (System.Exception ee)
			{
				logger.Error(ee);
				strResult = ee.Message;
			}

			return strResult;
		}
	}
}
